//
// @Auther: by wujehy 
// @Email:wujehy@qq.com
// @Data:20-5-18
// @Time:上午10:48
//

#ifndef SERIALSOCKET_SERIALCLIENT_H
#define SERIALSOCKET_SERIALCLIENT_H

#include <iostream>
#include <vector>
#include <string>
#include <boost/asio.hpp>

class SerialClient;

typedef void (*ReadFunction)(SerialClient *client, boost::asio::serial_port &port, const boost::system::error_code &ec,
                             size_t byte_read);

//typedef void (*WriteCallbackFunction)(SerialClient *client , );
class SerialClient {
    typedef std::vector<char> buffer_type;
private:
    boost::asio::io_service m_ios;
    boost::asio::serial_port port;
    boost::system::error_code ec;
    buffer_type m_buf;


public:
    explicit SerialClient(std::string portName, int baudrate = 9600, int flow_control = 0, int parity_type = 0,
                          int stop_type = 0);

    void read_buffer(ReadFunction function);

    static void handle_write(boost::asio::serial_port &port, const boost::system::error_code &ec);

    void write(std::string &msg);

    void run();

    void stop();

    const buffer_type &getMBuf() const;
};


#endif //SERIALSOCKET_SERIALCLIENT_H
